function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

/**
 * 简单函数防抖
 * @param action
 * @param delay
 * @returns {Function}
 */
export const debounce = function (fn, delay = 500) {
  var timer = null
  return function () {
    let context = this
    let args = arguments
    clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(context, args)
    }, delay)
  }
}

/**
 * 简单函数节流
 * @param action
 * @param delay
 * @returns {Function}
 */
export const throttle = function (fn, delay = 500) {
  let last = 0
  return function () {
    let current = Number(new Date())
    if (current - last > delay) {
      fn.apply(this, arguments)
      last = current
    }
  }
}

export default {
  formatNumber,
  formatTime
}
